package com.meichis.yndms.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.activity.TakePhotoActivity;
import com.google.android.entity.CameraParam;
import com.meichis.mcsappframework.dialog.CustomMessageDialog;
import com.meichis.mcsappframework.dialog.MyAlertDialog;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.ImageAdapter;
import com.meichis.yndms.adapter.TerminalFeedBackReplayAdapter;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.ActivityTerminalFeedbackDetailBinding;
import com.meichis.yndms.model.FeedBack;
import com.meichis.yndms.model.Picture;
import com.meichis.yndms.room.AppDatabase;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.mcsappframework.utils.AndroidEditUtil;
import com.meichis.yndms.vm.TerminalFeedbackDetailVm;

import java.util.ArrayList;
import java.util.UUID;


/**
 * @author : create by  szh
 * @date : 2022/11/10 17:22
 * @desc :终端反馈详情
 */

public class TerminalFeedbackDetailActivity extends BaseActivity {

    ActivityTerminalFeedbackDetailBinding binding;
    private ImageAdapter adapter;
    private TerminalFeedbackDetailVm vm;
    private int Id;

    private static final int REQUEST_TAKEPHOTO = 0;
    private ImageAdapter imageadapter;
    private TerminalFeedBackReplayAdapter replayAdapter;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_terminal_feedback_detail);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(TerminalFeedbackDetailVm.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
        AndroidEditUtil.assistActivity(this);

    }

    @Override
    protected void init() {
        vm.deleteFeedBackPic();
        binding.navigation.txtTitle.setText("终端反馈详情");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());


        Id = getIntent().getIntExtra(MCode.ID, 0);
        vm.GetMyFeedbackByID(Id);

        binding.rcImg.setLayoutManager(new GridLayoutManager(this, 3));
        binding.rcUpImage.setLayoutManager(new GridLayoutManager(this, 3));
        binding.rcReplay.setLayoutManager(new LinearLayoutManager(this));

        adapter = new ImageAdapter(this, R.layout.activity_signin_photoitem, new ArrayList<>());
        imageadapter = new ImageAdapter(this, R.layout.activity_signin_photoitem, vm.piclist);
        replayAdapter = new TerminalFeedBackReplayAdapter(this, R.layout.item_terminal_feedback_replay, new ArrayList<>());


        binding.rcImg.setAdapter(adapter);
        binding.rcUpImage.setAdapter(imageadapter);
        binding.rcReplay.setNestedScrollingEnabled(false);

        binding.rcReplay.setAdapter(replayAdapter);

        initEvent();
        addObserver();


    }

    private void initEvent() {
        imageadapter.setonDeleteListener(position -> {
            binding.ibtTakephoto.setVisibility(vm.piclist.size() < 6 ? View.VISIBLE : View.GONE);
            vm.piclist.remove(position);
            return null;
        });
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getFeedBackDetail().observe(this, feedBackDetail -> {
            ArrayList<Picture> pictures = new ArrayList<>();
            Picture picture;
            for (FeedBack.AttsBean att : feedBackDetail.getAtts()) {
                picture = new Picture();
                picture.setGuid(att.getGuid());
                pictures.add(picture);
            }
            adapter.setList(pictures);
            replayAdapter.setList(feedBackDetail.getReplys());

            if (feedBackDetail.getState() == 2) {
                binding.btNewReplay.setVisibility(View.VISIBLE);
                binding.btConfirmReplay.setVisibility(View.GONE);
                binding.llTodo.setVisibility(View.GONE);
            } else if (feedBackDetail.getState() == 3) {
                binding.btNewReplay.setVisibility(View.GONE);
                binding.btConfirmReplay.setVisibility(View.GONE);
                binding.llTodo.setVisibility(View.VISIBLE);
            } else {
                binding.btNewReplay.setVisibility(View.GONE);
                binding.btConfirmReplay.setVisibility(View.GONE);
                binding.llTodo.setVisibility(View.GONE);
            }
        });

        vm.getReplaySuccess().observe(this, aBoolean -> {
            if (aBoolean) {
                finish();
            }
        });

        vm.getFinishSuccess().observe(this, aBoolean -> {
            if (aBoolean) {
                finish();
            }
        });
    }

    public class ClickEvent {

        public void addReplay() {
            binding.llReplayBox.setVisibility(View.VISIBLE);
            binding.btNewReplay.setVisibility(View.GONE);
            binding.llTodo.setVisibility(View.GONE);
            binding.btConfirmReplay.setVisibility(View.VISIBLE);
        }

        public void onTakephoto() {
            Intent intent = new Intent(TerminalFeedbackDetailActivity.this, TakePhotoActivity.class);
            CameraParam takePhotoParam = new CameraParam(CameraParam.Quantity.Low);
            takePhotoParam.setWatermark("");
            intent.putExtra("IsNeedChooseFromAlbum", true);
            startActivityForResult(intent, REQUEST_TAKEPHOTO);
        }

        public void saveFeedBackResult() {
            vm.UploadFeedBackPicture();
        }

        public void finishFeedBack() {
            MessageTools.ShowmessageDialog(TerminalFeedbackDetailActivity.this, "是否确认完成该条记录,完成后将无法再次回复,请确认您已解决了门店反馈的问题！", () -> vm.finishFeedBack());
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_CANCELED) return;
        switch (requestCode) {
            case REQUEST_TAKEPHOTO:
                String picPath = data.getExtras().getString("result");
                if (!TextUtils.isEmpty(picPath)) {
                    Picture pic = new Picture();
                    pic.setFilePath(picPath);
                    pic.setGuid(UUID.randomUUID().toString());
                    pic.setRelateType(231);//终端反馈照片
                    pic.setRelateID(1);
                    pic.setFillName(picPath.substring(picPath.lastIndexOf("/") + 1));
                    AppDatabase.getInstance().pictureDao().insert(pic);
                    vm.piclist.add(pic);
                }
                if (vm.piclist.size() >= 5) {
                    binding.ibtTakephoto.setVisibility(View.GONE);
                }
                imageadapter.notifyDataSetChanged();
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
